package com.dayu.finecomm.protocol.cjj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayu.finecomm.protocol.codec.FixedLengthCheckMagicDecoder;
import com.dayu.finecomm.protocol.codec.Protocol;
import com.dayu.finecomm.protocol.pojo.TPacket;
import com.dayu.finecomm.protocol.pojo.TPacket.VType;
import com.dayu.finecomm.protocol.pojo.TSensor;
import com.dayu.finecomm.utils.Helpers;

import io.netty.buffer.ByteBuf;

/**
 * @ClassName: BNJDecoder
 * @Description:倍捻机协议
 * @author: 戴卫国
 * @date: 2018年2月23日 下午4:34:21
 * 
 * @Copyright: 2018 dayu Inc. All rights reserved.
 */
@Protocol(BNJDecoder.PROTO)
public class BNJDecoder extends FixedLengthCheckMagicDecoder<TPacket> {

	static final String PROTO = "BNJ";

	static Logger LOGGER = LoggerFactory.getLogger(BNJDecoder.class);

	public BNJDecoder() {

		super(new byte[] { (byte) 0x4c, (byte) 0x4c }, new byte[] { (byte) 0x4e, (byte) 0x4e }, 0, 2, 1);
	}

	@Override
	public int checkStart(ByteBuf in) {
		byte first = -1;
		byte second = 0;
		int index = 0;
		byte[] header = new byte[] { (byte) 0x4c, (byte) 0x4c };
		while (in.readableBytes() > 2) {
			if (first == -1) {
				first = in.readByte();
			} else {
				first = second;
			}
			second = in.readByte();
			if (Arrays.equals(new byte[] { first, second }, header)) {
				return index;
			}
			index += 1;
		}
		return -1;
	}

	@Override
	public TPacket unpack(ByteBuf in) {
		// TODO Auto-generated method stub
		in.skipBytes(2); // 4C 4C
		int length = in.readUnsignedByte() * 256 + in.readUnsignedByte(); // length
		long sTime = new Date().getTime() / 1000;
		TPacket s = new TPacket(VType.PPP, PROTO, "", sTime);
		s.sn = Helpers.bytesToHexString(in.readBytes(12));
		for (; in.readerIndex() <= length - 2;) {
			String sn = in.readUnsignedByte() + "";
			int type = in.readUnsignedByte();
			TPacket s1 = new TPacket(VType.PPP, PROTO, "", sTime);
			s1.sn = sn;
			s.packets.add(s1);
			switch (type) {
			case 0x00:// 实时报文
				int val = in.readUnsignedByte() * 256 + in.readUnsignedByte();
				s1.sensors.add(new TSensor(sn, "V", val, sTime));
				s1.sensors.add(new TSensor(sn, "K", in.readUnsignedByte(), sTime));
				break;
			case 0xAA:// 补报报文
				int size = in.readUnsignedByte();
				Map<String, List<Integer>> v = new HashMap<>();
				List<Integer> ls = new ArrayList<Integer>();
				List<Integer> vs = new ArrayList<Integer>();
				v.put("L", ls);
				v.put("V", vs);
				for (int i = 0; i < size; i++) {
					ls.add(in.readUnsignedByte() * 256 + in.readUnsignedByte());
					vs.add(in.readUnsignedByte() * 256 + in.readUnsignedByte());
				}
				s1.sensors.add(new TSensor(sn, "H", v, sTime));
				break;
			default:
				return null;
			}
			s1.sensors.add(new TSensor(sn, "S", in.readUnsignedByte(), sTime));
		}
		LOGGER.info("Midway:" + s.sn);
		return s;
	}

	@Override
	public byte[] echo() {
		// TODO Auto-generated method stub
		return null;
	}

}
